package cn.com.hhrcw.login;

import cn.com.hhrcw.entity.PiInfo;
import cn.com.hhrcw.entity.SysUser;
import cn.com.hhrcw.service.IPiInfoService;
import cn.com.hhrcw.service.ISysUserService;
import cn.hutool.core.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @program: recruit-boot-parent
 * @author: JD
 * @create: 2020-10-30 10:52
 **/
@Slf4j
public class WxOaLogin extends AbstractLogin<SysUser> {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IPiInfoService piInfoService;

    private final LoginVO vo;

    public WxOaLogin(String userid) {
        super(userid, "password");
        this.vo = new LoginVO();
        this.vo.setId(userid);

    }

    @Override
    public Object login() {
        SysUser member = userService.lambdaQuery().eq(SysUser::getId, this.vo.getId()).eq(SysUser::getDelFlag, false).one();
        Assert.notNull(member, "账号异常");

        String memberId = member.getId();
        PiInfo piInfo = piInfoService.lambdaQuery()
            .eq(PiInfo::getUserId, memberId)
            .eq(PiInfo::getDelFlag, false)
            .one();
        Assert.notNull(piInfo, "账号异常！");
        Assert.isFalse(piInfo.getBanFlag() != null && piInfo.getBanFlag().booleanValue(), "当前账号已被禁用，请联系相关客服进行处理");
        return member;
    }

    @Override
    public void success(SysUser user) {
        vo.setId(user.getId());
        vo.setMobile(user.getPhone());
        vo.setUsername(user.getUsername());
        vo.setVip(true);
        vo.setDesc("");
        vo.setToken(super.token);
        user.setToken(super.token);
        userService.updateById(user);
    }
}
